﻿using log4net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO.Ports;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using TruckScale.Models;
using TruckScale.Properties;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for SSHardwareView.xaml
  /// </summary>
  public partial class SSHardwareView : RadWindow, INotifyPropertyChanged
  {
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;
    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];

    public bool IOEnable { get; set; }
    public bool GratingEnable { get; set; }
    public bool QR1Enable { get; set; }
    public bool QR2Enable { get; set; }
    public bool RF1Enable { get; set; }
    public bool RF2Enable { get; set; }
    public bool AxleEnable { get; set; }
    //public bool Axle2Enable { get; set; }
    public bool LPR1Enable { get; set; }
    public bool LPR2Enable { get; set; }
    public bool Monitor1Enable { get; set; }
    public bool Monitor2Enable { get; set; }
    public bool Monitor3Enable { get; set; }
    public bool Monitor4Enable { get; set; }
    public bool LED1Enable { get; set; }
    public bool LED2Enable { get; set; }

    public string[] PortSource { get; set; } = SerialPort.GetPortNames();
    public string SelectedIOPort { get; set; }
    public string SelectedQR1Port { get; set; }
    public string SelectedQR2Port { get; set; }
    public string SelectedRF1Port { get; set; }
    public string SelectedRF2Port { get; set; }
    public string SelectedAxlePort { get; set; }
    //public string SelectedAxle2Port { get; set; }

    public string LPR1IP { get; set; }
    public string LPR2IP { get; set; }
    public string Monitor1IP { get; set; }
    public string Monitor2IP { get; set; }
    public string Monitor3IP { get; set; }
    public string Monitor4IP { get; set; }
    public string LED1IP { get; set; }
    public string LED2IP { get; set; }
    public string LPRUser { get; set; }
    public string LPRPwd { get; set; }
    public string MonitorUser { get; set; }
    public string MonitorPwd { get; set; }
    public string LEDWidth { get; set; }
    public string LEDHeihgt { get; set; }

    public SSHardwareView()
    {
      InitializeComponent();
      this.DataContext = this;

      //IO控制器串口
      IOEnable = Convert.ToBoolean(Global._hardwareConfig["IOEnable"]);
      SelectedIOPort = Global._hardwareConfig["SelectedIOPort"];
      GratingEnable = Convert.ToBoolean(Global._hardwareConfig["GratingEnable"]);

      //监控相机
      MonitorUser = Global._hardwareConfig["MonitorUser"];
      MonitorPwd = Global._hardwareConfig["MonitorPwd"];
      Monitor1Enable = Convert.ToBoolean(Global._hardwareConfig["Monitor1Enable"]);
      Monitor1IP = Global._hardwareConfig["Monitor1IP"];
      Monitor2Enable = Convert.ToBoolean(Global._hardwareConfig["Monitor2Enable"]);
      Monitor2IP = Global._hardwareConfig["Monitor2IP"];
      Monitor3Enable = Convert.ToBoolean(Global._hardwareConfig["Monitor3Enable"]);
      Monitor3IP = Global._hardwareConfig["Monitor3IP"];
      Monitor4Enable = Convert.ToBoolean(Global._hardwareConfig["Monitor4Enable"]);
      Monitor4IP = Global._hardwareConfig["Monitor4IP"];

      //车牌相机
      LPRUser = Global._hardwareConfig["LPRUser"];
      LPRPwd = Global._hardwareConfig["LPRPwd"];
      LPR1Enable = Convert.ToBoolean(Global._hardwareConfig["LPR1Enable"]);
      LPR1IP = Global._hardwareConfig["LPR1IP"];
      LPR2Enable = Convert.ToBoolean(Global._hardwareConfig["LPR2Enable"]);
      LPR2IP = Global._hardwareConfig["LPR2IP"];
    }

    protected override bool OnClosing()
    {
      Global._hardwareConfig["IOEnable"] = IOEnable.ToString();
      Global._hardwareConfig["SelectedIOPort"] = SelectedIOPort;
      Global._hardwareConfig["GratingEnable"] = GratingEnable.ToString();

      Global._hardwareConfig["MonitorUser"] = MonitorUser;
      Global._hardwareConfig["MonitorPwd"] = MonitorPwd;
      Global._hardwareConfig["Monitor1Enable"] = Monitor1Enable.ToString();
      Global._hardwareConfig["Monitor1IP"] = Monitor1IP;
      Global._hardwareConfig["Monitor2Enable"] = Monitor2Enable.ToString();
      Global._hardwareConfig["Monitor2IP"] = Monitor2IP;
      Global._hardwareConfig["Monitor3Enable"] = Monitor3Enable.ToString();
      Global._hardwareConfig["Monitor3IP"] = Monitor3IP;
      Global._hardwareConfig["Monitor4Enable"] = Monitor4Enable.ToString();
      Global._hardwareConfig["Monitor4IP"] = Monitor4IP;

      Global._hardwareConfig["LPRUser"] = LPRUser;
      Global._hardwareConfig["LPRPwd"] = LPRPwd;
      Global._hardwareConfig["LPR1Enable"] = LPR1Enable.ToString();
      Global._hardwareConfig["LPR1IP"] = LPR1IP;
      Global._hardwareConfig["LPR2Enable"] = LPR2Enable.ToString();
      Global._hardwareConfig["LPR2IP"] = LPR2IP;
      //保存到数据库
      HardwareConfigModel.UpdateConfig(Global._hardwareConfig);

      return base.OnClosing();
    }

    private void RadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      var button = sender as RadButton;
      if (button != null)
      {
        // 根据按钮的 Name 属性执行不同的操作
        switch (button.Name)
        {
          case "uartBtn":
            Process.Start("Tools\\UartAssist.exe");
            break;
          case "rfBtn":
            Process.Start("Tools\\RFID\\RFIDDemo.exe");
            break;
          case "lprBtn":
            Process.Start("Tools\\LPR\\LPRConfigTool.exe");
            break;
          case "monitorBtn":
            Process.Start("Tools\\SADPTool.exe");
            break;
          case "ledBtn":
            Process.Start("Tools\\LEDPlayer.exe");
            break;
        }
      }
    }
  }
}
